﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using Beyond;

public class SelectLevelController : BaseController
{
    public Text worldName, collectTxt, totalStarTxt,toLockText;
    public GameObject lockedContent;
    public int currWorld;
    public Transform levelContent;
    public GameObject levelButtonPrefab;
    public Button freeRuby;
    bool videoReady;
    public GameObject selectWorld, selectLevel;
    public List<GameObject> worldUnlock;
    public List<Text> progress;

    public static SelectLevelController Instance;

    protected override void Awake()
    {
        base.Awake();
        Instance = this;
    }

    protected override void Start()
    {
        base.Start();

        ZJ_WangSir.CUtils.CloseBannerAd();
        currWorld = LevelController.GetUnlockWorld();
        currWorld = Mathf.Clamp(currWorld, 1, Const.NUM_WORLD);
        
        UpdateInfor();
        UpdateTotalStar();

        freeRuby.onClick.AddListener(FreeRubyClick);
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape) && !DialogController.instance.IsDialogShowing())
        {
            if (SceneManager.GetActiveScene().name == "SelectLevel")
            {

#if UNITY_ANDROID
               CUtils.LoadScene(1);
#endif
            }
        }

        //if (Ads.isReady(Ads.Type.video))
        //{
        //    freeRuby.gameObject.SetActive(true);
        //}
        //else
        //{
        //    freeRuby.gameObject.SetActive(false);
        //}

    }


    public void UpdateTotalStar()
    {
        int total = Superpow.Utils.CalculateTotalStar();
        totalStarTxt.text = total + "/" + Superpow.Utils.GetTotalLevels() * 3;
    }

    public void UpdateInfor()
    {
        for (int i = levelContent.childCount - 1; i >= 0; i--)
        {
            DestroyImmediate(levelContent.GetChild(i).gameObject);
        }

        int numLevels = Superpow.Utils.GetNumLevels(currWorld);
        for (int i = 0; i < numLevels; i++)
        {
            GameObject levelButton = Instantiate(levelButtonPrefab);
            levelButton.transform.SetParent(levelContent);
            levelButton.transform.localScale = Vector3.one;
        }

        int unlockWorld = LevelController.GetUnlockWorld();
        lockedContent.SetActive(currWorld > unlockWorld);

        for (int i = 0; i < worldUnlock.Count; i++)
        {
            if(unlockWorld>worldUnlock[i].transform.parent.GetSiblingIndex())
            {
                worldUnlock[i].SetActive(true);
            }
        }

        for (int i = 0; i < progress.Count; i++)
        {
            progress[i].text = LevelController.GetUnlockLevel(i + 1) + "/15";
        }

        if (unlockWorld >= currWorld)
           LevelController.SetCurrentWorld(currWorld);

        if (MySetLanguage.GetLanguage().StartsWith("Chinese"))
        {
            if (MySetLanguage.GetLanguage() == "ChineseTraditional")
            {
                worldName.text = Const.WORLD_NAMES_FT[currWorld - 1];
                collectTxt.text = "收集 " + Const.UNLOCK_STARS[currWorld - 1];
                toLockText.text = "可解鎖";
            }

            else
            {
                worldName.text = Const.WORLD_NAMES_JT[currWorld - 1];
                collectTxt.text = "收集 " + Const.UNLOCK_STARS[currWorld - 1];
                toLockText.text = "可解锁";
            }

        }
        else
        {
            worldName.text = Const.WORLD_NAMES_EN[currWorld - 1];
            collectTxt.text = "Collect " + Const.UNLOCK_STARS[currWorld - 1];
            toLockText.text = "TO UNLOCK WORLD";
        }


        foreach (Transform tf in levelContent)
        {
            tf.GetComponent<LevelButton>().UpdateLevelState();
        }
    }

    public void OnNextWorld()
    {
        if (currWorld < Const.NUM_WORLD)
        {
            Sound.instance.PlayButton();
            currWorld++;
            UpdateInfor();
        }
    }

    public void OnPrevWorld()
    {
        if (currWorld > 1)
        {
            Sound.instance.PlayButton();
            currWorld--;
            UpdateInfor();
        }
    }

    public void OnPlayClick()
    {
        Sound.instance.PlayButton();
        Superpow.Utils.SetGameMode(GameMode.CHALLENGE_MODE);
        int unlockLvl = LevelController.GetUnlockLevel(currWorld);
        LevelController.SetCurrentLevel(currWorld, unlockLvl);
        CUtils.LoadScene(3);
    }

    public void OnShopClick()
    {
        DialogController.instance.ShowDialog(DialogType.Shop);
    }

    public void FreeRubyClick()
    {
        AdsProxy.PlayVideoByType(AdsProxy.VideoType.VideoToGetRuby);
    }

    public void ClickWorld(int num)
    {
        Sound.instance.PlayButton();
        currWorld = num;
        UpdateInfor();
        selectWorld.SetActive(false);
        selectLevel.SetActive(true);
    }

    public void SelectWorldBack()
    {
        InsAdEvaluate.Instance.OnlyInAdWithoutToggle();
        CUtils.LoadScene(1);
    }

    public void SelectLevelBack()
    {
        selectWorld.SetActive(true);
        selectLevel.SetActive(false);
    }
}
